$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'api/bbudgetstandard/list',
        datatype: "json",
        colModel: [
            {label: 'guid', name: 'guid', index: 'guid', width: 50, key: true, hidden: true},
            {label: '发布年度', name: 'bSYear', index: 'b_s_year', width: 80},
            {label: '标准名称', name: 'bSName', index: 'b_s_name', width: 80},
            {label: '分册', name: 'bSDivision', index: 'b_s_division', width: 80},
            {label: '状态', name: 'bSState', index: 'b_s_state', width: 80},
            {
                label: '操作', name: '', width: 180, formatter: function (cellvalue, options, rowObject) {
                console.log(rowObject)

                return "<a  class='btn btn-primary' onclick='show(\"" + rowObject.guid + "\",1)' ></i>&nbsp;预算项</a>"
                    + "&nbsp;&nbsp;<a  class='btn btn-primary' onclick='show(\"" + rowObject.guid + "\",2)' ></i>&nbsp;准入条件</a>"
                    + "&nbsp;&nbsp;<a  class='btn btn-primary' onclick='show(\"" + rowObject.guid + "\",3)' ></i>&nbsp;其他工程量</a>"
                    + "&nbsp;&nbsp;<a  class='btn btn-primary' onclick='show(\"" + rowObject.guid + "\",4)' ></i>&nbsp;交叉预算表</a>"
            }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

function show(a, i) {
    if (i == 1) {
        layer.open({
            type: 2,
            title: '预算项',
            shadeClose: true,
            shade: 0.5,
            maxmin: true, //开启最大化最小化按钮
            area: ['90%', '90%'],
            content: 'bbudgetitem.html?aguid=' + a
        });
    }else if(i ==2){
        layer.open({
            type: 2,
            title: '准入条件',
            shadeClose: true,
            shade: 0.5,
            maxmin: true, //开启最大化最小化按钮
            area: ['90%', '90%'],
            content: 'bentrycriteria.html?aguid=' + a
        });
    }else if(i ==3){
        layer.open({
            type: 2,
            title: '其他工程量',
            shadeClose: true,
            shade: 0.5,
            maxmin: true, //开启最大化最小化按钮
            area: ['90%', '90%'],
            content: 'botherquantities.html?aguid=' + a
        });
    }else if(i ==4){
        layer.open({
            type: 2,
            title: '交叉预算表',
            shadeClose: true,
            shade: 0.5,
            maxmin: true, //开启最大化最小化按钮
            area: ['90%', '90%'],
            content: 'bcrossquantities.html?aguid=' + a
        });
    }

}

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        bBudgetStandard: {},
        status: ["在用", "停用"]
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.bBudgetStandard = {};
        },
        update: function (event) {
            var guid = getSelectedRow();
            if (guid == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(guid)
        },
        saveOrUpdate: function (event) {
            var url = vm.bBudgetStandard.guid == null ? "api/bbudgetstandard/save" : "api/bbudgetstandard/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.bBudgetStandard),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var guids = getSelectedRows();
            if (guids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "api/bbudgetstandard/delete",
                    contentType: "application/json",
                    data: JSON.stringify(guids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (guid) {
            $.get(baseURL + "api/bbudgetstandard/info/" + guid, function (r) {
                vm.bBudgetStandard = r.bBudgetStandard;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }
    }
});